import json
from loguru import logger
import uuid
class EvolutionManager:
    def __init__(self, root_agent_instance):
        self.root_agent = root_agent_instance
        self.gmemory = root_agent_instance.gmemory
        self.self_evolution_agent = root_agent_instance.self_evolution_agent

    def initiate_evolution_task(self, userid: str, openid: str, command: str = None):
        logger.info("Initiating self-evolution task.")
        # Retrieve insights from GMemory to guide evolution
        if command:
            _, _, insights = self.gmemory.retrieve_memory(query_task=command)
        else:
            # If no specific command, get general insights
            insights = self.gmemory.insights_layer.query_insights_with_score("general AI assistant improvement", top_k=10)
            insights = [insight for insight, score in insights]

        evolution_request = {
            "protocol_version": "1.0",
            "task_id": str(uuid.uuid4()),
            "requester": "RootAgent",
            "target_agent": "SelfEvolutionAgent",
            "task": {
                "goal": None, # RootAgent doesn't set a specific goal for evolution, SelfEvolutionAgent determines it
                "insights": insights,
                "file_to_modify": None # RootAgent doesn't specify a file to modify for evolution
            }
        }
        self.self_evolution_agent.process_command(json.dumps(evolution_request))
